<?php
/**
 * Created by PhpStorm.
 * User: yf
 * Date: 2018/5/28
 * Time: 下午6:33
 */

namespace EasySwoole\EasySwoole;


use App\Process\HotReload;
use App\Crontab\TaskOne;
use App\Utility\ConsoleCommand\StaticBonus;
use App\Utility\Pool\MysqlPool;
use App\Utility\Pool\RedisPool;
use EasySwoole\Component\Pool\PoolManager;
use EasySwoole\EasySwoole\Crontab\Crontab;
use EasySwoole\EasySwoole\Swoole\EventRegister;
use EasySwoole\EasySwoole\AbstractInterface\Event;
use EasySwoole\Http\Request;
use EasySwoole\Http\Response;
use EasySwoole\Utility\File;

class EasySwooleEvent implements Event
{

    public static function initialize()
    {
        // TODO: Implement initialize() method.
        date_default_timezone_set( 'Asia/Shanghai' );

        require_once EASYSWOOLE_ROOT . "/App/Utility/simple_dom_html.php";
        self::loadConf();
        /**
         * 系统前端,后台,api接口域名定义
         */
        defined( 'WEB_URL' ) or define( 'WEB_URL', 'http://www.mxtang.net' );#前端
        defined( 'ADMIN_URL' ) or define( 'ADMIN_URL', 'http://admin.mxtang.net/' );#后台
        defined( 'API_URL' ) or define( 'API_URL', 'http://api.mxtang.net/' );#接口
        defined( 'QINIU_URL' ) or define( 'QINIU_URL', 'http://file.mxtang.net/' );#七牛存储域名



    }

    /**
     * 加载配置文件
     */
    public static function loadConf()
    {
        $files = File::scanDirectory( EASYSWOOLE_ROOT . '/App/Config' );
        if ( is_array( $files ) ) {
            foreach ( $files['files'] as $file ) {
                $fileNameArr = explode( '.', $file );
                $fileSuffix = end( $fileNameArr );
                if ( $fileSuffix == 'php' ) {
                    Config::getInstance()->loadFile( $file );
                } elseif ( $fileSuffix == 'env' ) {
                    Config::getInstance()->loadEnv( $file );
                }
            }
        }
    }

    public static function mainServerCreate( EventRegister $register )
    {

        // TODO: Implement mainServerCreate() method.
        // 自定义进程注册例子
//        $swooleServer = ServerManager::getInstance()->getSwooleServer();
//        //自适应热重启 虚拟机下可以传入 disableInotify => true 强制使用扫描式热重启 规避虚拟机无法监听事件刷新
//        $swooleServer->addProcess( ( new HotReload( 'HotReload', ['disableInotify' => false] ) )->getProcess() );

        //注册mysql连接池
        PoolManager::getInstance()->register( MysqlPool::class, Config::getInstance()->getConf( 'MYSQL.POOL_MAX_NUM' ) )->setMinObjectNum( (int)Config::getInstance()->getConf( 'MYSQL.POOL_MIN_NUM' ) );
        // 注册redis连接池
        PoolManager::getInstance()->register( RedisPool::class, Config::getInstance()->getConf( 'REDIS.POOL_MAX_NUM' ) )->setMinObjectNum( (int)Config::getInstance()->getConf( 'REDIS.POOL_MIN_NUM' ) );
        //创建临时目录    请以绝对路径，不然守护模式运行会有问题
        $uploadDir = Config::getInstance()->getConf( 'UPLOAD_DIR' );
        if ( empty( $uploadDir ) ) {
            $uploadDir = EASYSWOOLE_ROOT . '/Upload';
            Config::getInstance()->setConf( 'UPLOAD_DIR', $uploadDir );
        }
        if ( !is_dir( $uploadDir ) ) {
            mkdir( $uploadDir );
        }

        /**
         * **************** Crontab任务计划 **********************
         */
        // 开始一个定时任务计划
        Crontab::getInstance()->addTask(TaskOne::class);
    }

    public static function onRequest( Request $request, Response $response ): bool
    {
        // TODO: Implement onRequest() method.
        //为每个请求做标记
        return true;
    }

    public static function afterRequest( Request $request, Response $response ): void
    {
        // TODO: Implement afterAction() method.
    }

    public static function onReceive( \swoole_server $server, int $fd, int $reactor_id, string $data ): void
    {

    }

}